package com.lzj.controller;

import com.lzj.dao.EmployeeMapper;
import com.lzj.entity.Department;
import com.lzj.entity.Employee;
import com.lzj.service.DepartmentService;
import com.lzj.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 *登录
 * </p>
 *
 * @autor:lzj
 * @date:2022/2/26
 */
@Controller
public class LoginController {
    @Autowired
    EmployeeService employeeService;
    @Autowired
    DepartmentService departmentService;

    @RequestMapping("/")
    public String login(){
        return "login";
    }

    @PostMapping("/doLogin")
    public String doLogin(String username, String password, Model model, HttpSession session){
        Employee employee = employeeService.loadEmpByUsername(username);
        if(employee==null||!employee.getPassword().equals(password)){
            model.addAttribute("error","用户名或密码输入错误，登陆失败");
            return "forward:/";
        }else {//判断用户状态
            if (employee.getStatus().equals("0")){
                model.addAttribute("error","用户待审批");
                return "forward:/";
            }else if(employee.getStatus().equals("2")){
                model.addAttribute("error","用户审批未通过");
                return "forward:/";
            }else{
                session.setAttribute("currentuser",employee);
                Employee currentuser = (Employee) session.getAttribute("currentuser");
                return "redirect:/notifications/"+employee.getEmployeeid();
            }
        }
    }

    @RequestMapping("/register")
    public String register(Model model){
        List<Department> allDeps = departmentService.getAllDeps();
        model.addAttribute("deps",allDeps);
        return "register";
    }

    @PostMapping("/doReg")
    public String doReg(Employee employee,Model model){
        System.out.println(employee);
        int i = employeeService.doReg(employee);
        if (i==1){
            return "redirect:/";
        }else if(i==-1){
            model.addAttribute("msg","用户名重复");
            model.addAttribute("employee",employee);
            return "forward:/register";
        }else{
            model.addAttribute("employee",employee);
            model.addAttribute("error","注册失败");
            return "forward:/register";
        }
    }

    @RequestMapping("/changepassword")
    public String changepassword(){
        return "changepassword";
    }

    @PostMapping("/doNew")
    public String doNew(String oldpassword,String newpassword,HttpSession session,Model model){
        Employee currentuser = (Employee) session.getAttribute("currentuser");
        if(oldpassword.equals(currentuser.getPassword())){
            employeeService.updatePassword(newpassword,currentuser.getEmployeeid());
            session.removeAttribute("currentuser");
            return "redirect:/";
        }else{
            model.addAttribute("error","旧密码输入错误");
            return "forward:/changepassword";
        }
    }
}
